<?php
/**
 * 友情链接 控制器
 * */
class FriendlinksAction extends Action
{
    /**
     * 管理 友情链接
     * */
    public function AdminIndex()
    {
        EasyACL('Friendlinks','AdminIndex',$_SESSION);
        $links = D('Friendlinks');
        $count = $links->count(NULL,'fid'); //计算
        $listRows = 15; //行数
        $fields = 'fid,cid,name,show,links_order'; //显示列
        
        import('ORG.Util.Page');
        $p = new Page($count,$listRows);
        $list = $links->xfindall(NULL,$fields,'links_order desc,fid desc',$p->firstRow .','.$p->listRows);
        $page = $p->show();

        //设置到模板
        $this->assign('links',$list);
        $this->assign('page',$page);
        $this->display();
    }

    /**
     * 创建友情链接
     * */
    public function AdminCreate()
    {
        EasyACL('Friendlinks','AdminCreate',$_SESSION);
        //POST
        if ($this->isPost()) {
            $Links = D('Friendlinks');
            if ($Links->create()) {
                if ($Links->add()) {
                    $id = $Links->getLastInsId();
                    //$this->redirect('AdminIndex','Friendlinks');
                    if ($_FILES['file']['name'] != '') {
                        $this->UpImg($id);
                    } else {
                        //如果没有选择上传图片，则直接进行跳转到友情链接列表页面
                        $this->redirect('AdminIndex','Friendlinks');
                    }
                } else {
                    return $this->error($Links->getError());
                }
            } else {
                return $this->error($Links->getError());
            }
        }
        //获取所有的友情链接分类
        $Class = D('Friendlinksclass');
        $C = $Class->findall();

        $this->assign('C',$C);
        $this->display();
    }
    /**
     * 修改友情链接信息
     * */
    public function AdminUpdate()
    {
        EasyACL('Friendlinks','AdminUpdate',$_SESSION);
        $Input = Input::getInstance();
        $Input->filter('addslashes');

        $links = D('Friendlinks');

        import('ORG.Util.HashMap');
        //POST
        if ($this->isPost()) {
            $conditions = new HashMap();
            $conditions->put('fid',array('EQ',$Input->post('fid')));
            $links->where($conditions)->find();
            if ($links->create()) {
                if ($links->save()) {
                    //return $this->success('修改友情链接资料成功');
                    if ($_FILES['file']['name'] != '') {
                        $this->UpImg($Input->post('fid'));
                    } else {
                        return $this->redirect('AdminIndex','Friendlinks');
                    }
                } else {
                    return $this->error($links->getError());
                }
            } else {
                return $this->error($links->getError());
            }
        }

        //获取要修改的友情链接信息
        $conditions = new HashMap();
        $conditions->put('fid',array('EQ',$Input->get('fid')));
        $L = $links->where($conditions)->find(); //获取要修改的连接信息
        
        //获取所有连接分类
        $Class = D('Friendlinksclass');
        $C = $Class->findall();

        //设置到模板
        $this->assign('C',$C); //连接分类
        $this->assign('L',$L); //连接信息
        $this->display();
    }
    /**
     * 删除友情链接
     * */
    public function AdminDel($id)
    {
        EasyACL('Friendlinks','AdminDel',$_SESSION);
        $links = D('Friendlinks');
        $conditions = array();
        $conditions['fid'] = $id;
        $L = $links->where($conditions)->find();
        @unlink($L['links_img']);
        if ($links->deleteById($id)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    public function AdminDels()
    {
        EasyACL('Friendlinks','AdminDels',$_SESSION);
        //POST
        if ($this->isPost()) {
            $fid = $_POST['fid'];
            $Error = '';
            foreach($fid as $id) {
                if (!$this->AdminDel($id)) {
                    $Error .= '尝试删除编号为'.$id.'友情链接失败<br />';
                }
            }
            if (empty($Error)) {
                $this->redirect('AdminIndex','Friendlinks');
            } else {
                $this->error($Error);
            }
        }
        //GET
        if ($this->isGet()) {
            $Input = Input::getInstance();
            $Input->filter('addslashes');
            if($this->AdminDel($Input->get('fid'))) {
                $this->redirect('AdminIndex','Friendlinks');
            } else {
                $this->error('尝试删除友情链接失败!');
            }
        }
    }
    /**
     * 上传友情链接图片
     * */
    public function UpImg($id)
    {
        EasyACL('Friendlinks','UpImg',$_SESSION);
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();
		//设置上传文件大小
		$upload->maxSize  = 266240;
		//设置上传文件类型
		$upload->allowExts  = explode(',','jpg,gif,png');
		//设置附件上传目录
		$upload->savePath =  './upload/';

		//执行上传操作
		if(!$upload->upload()) {
			return FALSE;
		}else{
			$File = $upload->getSuccessFiles();
            import('ORG.Util.HashMap');
            $Conditions = new HashMap();
            $Conditions->put('fid',array('EQ',$id));

            $links = D('Friendlinks');
            $links->where($Conditions)->find();
            $links->links_img = $File[0]['savepath'] . $File[0]['savename'];
            if ($links->save()) {
                $this->redirect('AdminIndex','Friendlinks');
            } else {
                $this->error('尝试上传友情链接图片失败!');
            }
		}
    }
}
?>
